<?php

use Biz\cache\wiki\Wiki;
use Biz\model\SamsungActivity\SamsungJoinUserListModel;
use Doraemon\tools\Log;

class SaveAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;

    /**
     * @throws \Exception
     */
    public function main()
    {
        $currUid = $this->uid;
        $name = trim($this->getRequest()->getPost('name', ''));//姓名
        $nick = trim($this->getRequest()->getPost('nick', ''));//好好住昵称
        $avatar = trim($this->getRequest()->getPost('avatar', ''));//好好住昵称
        $phone = trim($this->getRequest()->getPost('phone', ''));//手机号
        $profession = $this->getRequest()->getPost('profession_id', 0);//职业
        $agency = trim($this->getRequest()->getPost('agency', ''));//机构
        $idea = trim($this->getRequest()->getPost('idea', ''));//设计理念
        $desc = trim($this->getRequest()->getPost('desc', ''));//描述
        $imgs = trim($this->getRequest()->getPost('imgs', ''));//设计图
        $imgsOriUrl = trim($this->getRequest()->getPost('imgs_ori_url', ''));//设计图原图地址
        $tvType = $this->getRequest()->getPost('tv_type_id', '');//电视型号
        $artistId = $this->getRequest()->getPost('artist_id', '');//所选艺术家

        //参数验证
        if (!isset(SamsungJoinUserListModel::ARTIST_MAP[$artistId - 1])) {
            throw new \Exception('参数错误，请重新选择艺术家');
        }

        if ($currUid == 0 && (empty($name) || empty($phone))) {
            throw new \Exception('参数错误，姓名以及手机号必填');
        }

        if (!isset(SamsungJoinUserListModel::TV_TYPE_MAP[$tvType - 1])) {
            throw new \Exception('参数错误，请重新选择电视型号');
        }

        if (empty($idea)) {
            throw new \Exception('参数错误，请填写设计理念');
        }
        if (mb_strlen($idea, 'utf8') > 300) {
            throw new Exception('设计理念最多可填写300个字');
        }

        if (empty($desc)) {
            throw new \Exception('参数错误，请填写三星电视艺术阐述');
        }
        if (mb_strlen($desc, 'utf8') > 150) {
            throw new Exception('三星电视艺术阐述最多可填写150个字');
        }

        if (empty($imgs)) {
            throw new \Exception('参数错误，请上传设计图');
        } else {
            $coverImg = json_decode($imgs)[0];

        }

        $data = [
            'uid' => $currUid,
            'name' => $name,
            'nick' => $nick,
            'avatar' => $avatar,
            'phone' => $phone,
            'profession_id' => $profession,
            'agency' => $agency,
            'idea' => $idea,
            'desc' => $desc,
            'imgs' => $imgs,
            'imgs_ori_url' => $imgsOriUrl,
            'cover_img' => $coverImg,
            'tv_type_id' => $tvType,
            'artist_id' => $artistId,
        ];
        //禁止重复提交 5秒内
        $lockKey = md5(json_encode($data));
        $lock = Wiki::isLock($lockKey, 5);
        if ($lock) {
            throw new \Exception('操作过于频繁，请稍后再试');
        }
        $insertId = SamsungJoinUserListModel::insertData($data);
        Log::info("SamsungJoinUserList-Save", $data);
        $this->setSuccess(['id' => $insertId], self::API_CODE_SUCCES);

    }
}